<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类id" prop="catid">
        <el-input
          v-model="queryParams.catid"
          placeholder="请输入分类id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家id" prop="merchantid">
        <el-input
          v-model="queryParams.merchantid"
          placeholder="请输入商家id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名字" prop="gname">
        <el-input
          v-model="queryParams.gname"
          placeholder="请输入商品名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品图片" prop="gimg">
        <el-input
          v-model="queryParams.gimg"
          placeholder="请输入商品图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售价" prop="saleprice">
        <el-input
          v-model="queryParams.saleprice"
          placeholder="请输入销售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细标题" prop="detailtitle">
        <el-input
          v-model="queryParams.detailtitle"
          placeholder="请输入详细标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="份量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入份量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布状态" prop="publishstate">
        <el-input
          v-model="queryParams.publishstate"
          placeholder="请输入发布状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="礼品点" prop="giftpoint">
        <el-input
          v-model="queryParams.giftpoint"
          placeholder="请输入礼品点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="验证状态" prop="verifystate">
        <el-input
          v-model="queryParams.verifystate"
          placeholder="请输入验证状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品id" align="center" prop="gid" />
      <el-table-column label="分类id" align="center" prop="catid" />
      <el-table-column label="商家id" align="center" prop="merchantid" />
      <el-table-column label="商品名字" align="center" prop="gname" />
      <el-table-column label="商品图片" align="center" prop="gimg" >
        <template slot-scope="scope">
          <img :src="$picbaserurl + scope.row.gimg" :width="50" :height="50">
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="销售价" align="center" prop="saleprice" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="详细标题" align="center" prop="detailtitle" />
      <el-table-column label="份量" align="center" prop="weight" />
      <el-table-column label="发布状态" align="center" prop="publishstate" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.publishstate=='0'" type="danger">未发布</el-tag>
          <el-tag v-else>已发布</el-tag>
        </template>
      </el-table-column>


      <el-table-column label="礼品点" align="center" prop="giftpoint" />
      <el-table-column label="验证状态" align="center" prop="verifystate" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.verifystate=='0'" type="danger">未验证</el-tag>
          <el-tag v-else>已验证</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="商品分类" align="center" prop="goodscategory" />


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    
    <el-pagination
        small
        v-show ="total>0"
        :total = "total"
        :current-page.sync="queryParams.pageNum"
        :page-size.sync="queryParams.pageSize"
        background
        layout="prev, pager, next"
        @current-change = "getList" />

    <!-- 添加或修改goods对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品分类">
          <el-select v-model="form.catid" placeholder="请选择商品分类" >
         <el-option v-for="index in catageory" :value="index.value" :label="index.name" :key="index.value"></el-option>
      
        </el-select>
        </el-form-item>
        <el-form-item label="商家id" prop="merchantid">
          <el-input v-model="form.merchantid" placeholder="请输入商家id" />
        </el-form-item>
        <el-form-item label="商品名字" prop="gname">
          <el-input v-model="form.gname" placeholder="请输入商品名字" />
        </el-form-item>
        <el-form-item label="商品图片" prop="gimg">
            <el-upload
        class="avatar-uploader"
        :action="$picbaserurl + '/upload'"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
       :before-upload="beforeAvatarUpload">
       <img v-if="form.gimg"
            :src="$picbaserurl + form.gimg"
           class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    


        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="销售价" prop="saleprice">
          <el-input v-model="form.saleprice" placeholder="请输入销售价" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="详细标题" prop="detailtitle">
          <el-input v-model="form.detailtitle" placeholder="请输入详细标题" />
        </el-form-item>
        <el-form-item label="份量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入份量" />
        </el-form-item>
        <el-form-item label="发布状态" prop="publishstate">
          <el-input v-model="form.publishstate" placeholder="请输入发布状态" />
        </el-form-item>
        <el-form-item label="礼品点" prop="giftpoint">
          <el-input v-model="form.giftpoint" placeholder="请输入礼品点" />
        </el-form-item>
        <el-form-item label="验证状态" prop="verifystate">
          <el-input v-model="form.verifystate" placeholder="请输入验证状态" />
        </el-form-item>
        <el-form-item label="商品分类" prop="goodscategory">
          <el-input v-model="form.goodscategory" placeholder="请输入商品分类" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/goods";
import { listCategory } from "@/api/category";

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // goods表格数据
      goodsList: [],
      catageory:[],
      isshow:1,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        catid: null,
        merchantid: null,
        gname: null,
        gimg: null,
        price: null,
        saleprice: null,
        description: null,
        detailtitle: null,
        weight: null,
        publishstate: null,
        giftpoint: null,
        verifystate: null,
        goodscategory:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询goods列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.data.rows;
        this.total = response.data.totalsize;
        console.log(this.total)
        this.loading = false;
      });
      listCategory(this.isshow).then(res =>{
          res.data.rows.forEach(element => {
            this.catageory.push({name:element.description,value:element.catid})
          })
            
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gid: null,
        catid: null,
        merchantid: null,
        gname: null,
        gimg: null,
        price: null,
        saleprice: null,
        description: null,
        detailtitle: null,
        weight: null,
        publishstate: null,
        giftpoint: null,
        verifystate: null,
        goodscategory:null,
      };
      this.$nextTick(() =>{
          this.resetForm("form");
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$nextTick(() =>{
        this.resetForm("queryForm").resetFields();
      })
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加goods";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gid = row.gid || this.ids
      getGoods(gid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改goods";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gid != null) {
            updateGoods(this.form).then(response => {
              this.$message({type:'success',message:'修改成功'})
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$message({type:'success',message:'新增成功'})
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gids = row.gid || this.ids;
      this.$confirm('是否确认删除banners编号为"' + gids + '"的数据项？','提示',{confirmButtonText:'确定',cancelButtonText:'取消',type:'warning'}).then(function(){
        return delGoods(gids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    },
    handleAvatarSuccess(res, file) {
      this.form.gimg = res.data;
      console.log(this.form.gimg);
    },
    beforeAvatarUpload(file) {
    
   
  }, 
  }
};
</script>
